package cn.car.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.googlecode.protobuf.format.XmlFormat;

import cn.car.Util.CarTool;
import cn.car.Util.StringUtils;
import cn.car.dao.ParkInfoDAO;
import cn.protos.car.CARProtos.CARROOT;
import cn.protos.car.CARProtos.CommonReq;
import cn.protos.car.CARProtos.ParkInfo;
import cn.protos.car.CARProtos.SessionBody;

@SuppressWarnings("unchecked")
public class ParkInfoDAOImpl implements ParkInfoDAO {
	private static final Log log = LogFactory.getLog(ParkInfoDAOImpl.class);
	private JdbcTemplate jdbcTemplate;

	public CARROOT getParkInfoList(CommonReq commonReq) {
		String pageSize = commonReq.getPageSize();
		String currentPage = commonReq.getCurrentPage();
		
		String sql = "select * from parkinfo where 1=1 ";
		sql += this.getConditionSQL(commonReq);
		sql += this.getOrderSQL();
		if(pageSize != null && currentPage != null) {
			sql += " limit " + Integer.parseInt(pageSize)*(Integer.parseInt(currentPage)-1) + "," + pageSize;
		}
		
		CARROOT.Builder builder = CarTool.createRspRootBuilder("0", "0", "0");
		SessionBody.Builder body = SessionBody.newBuilder();
		
		List<ParkInfo.Builder> recordList = new ArrayList<ParkInfo.Builder>();
		
		recordList = this.getJdbcTemplate().query(sql, new RowMapper() {
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				ParkInfo.Builder info = ParkInfo.newBuilder();
				info.setParkInfoId(StringUtils.nullToString(rs.getString("parkinfoid")));
				info.setName(StringUtils.nullToString(rs.getString("name")));
				info.setStreet(StringUtils.nullToString(rs.getString("street")));
				info.setDirection(StringUtils.nullToString(rs.getString("direction")));
				info.setReference(StringUtils.nullToString(rs.getString("reference")));
				info.setCrossing(StringUtils.nullToString(rs.getString("crossing")));
				info.setTotal(StringUtils.nullToString(rs.getString("total")));
				info.setRemain(StringUtils.nullToString(rs.getString("remain")));
				info.setTime(StringUtils.nullToString(rs.getString("time")));
				info.setPhotoUrl(StringUtils.nullToString(rs.getString("photourl")));
				info.setLatitudeE6(StringUtils.nullToString(rs.getString("latitudee6")));
				info.setLongitudeE6(StringUtils.nullToString(rs.getString("longitudee6")));
				info.setPhone(StringUtils.nullToString(rs.getString("phone")));
				info.setPrice(StringUtils.nullToString(rs.getString("price")));
				info.setRemark(StringUtils.nullToString(rs.getString("remark")));
				return info;
			}
		});
		
		for(ParkInfo.Builder record : recordList) {
			body.addParkInfo(record);
		}
		builder.setSessionBody(body);
		if (log.isInfoEnabled()) { 
			log.info(" ------ output begin ------  ");
			if(builder!=null){
				log.info(XmlFormat.printToString(builder.build()));
			}
			log.info(" ------ output end   ------  ");
		}
		return builder.build();
	}

	public int getParkInfoSize(CommonReq commonReq) {
		return 0;
	}
	
	public String getOrderSQL() {
		return " order by name";
	}
	
	public String getConditionSQL(CommonReq commonReq) {
		StringBuilder condition = new StringBuilder();
//		if(!StringUtils.nullToString(name).equals("")) {
//			condition.append(" and name='").append(name).append("'");
//		}
//		if(!StringUtils.nullToString(stree).equals("")) {
//			condition.append(" and stree='").append(stree).append("'");
//		}
//		if(!StringUtils.nullToString(direction).equals("")) {
//			condition.append(" and direction='").append(direction).append("'");
//		}
//		if(!StringUtils.nullToString(reference).equals("")) {
//			condition.append(" and reference='").append(reference).append("'");
//		}
//		if(!StringUtils.nullToString(crossing).equals("")) {
//			condition.append(" and crossing='").append(crossing).append("'");
//		}
//		if(!StringUtils.nullToString(total).equals("")) {
//			condition.append(" and total='").append(total).append("'");
//		}
//		if(!StringUtils.nullToString(remain).equals("")) {
//			condition.append(" and remain='").append(remain).append("'");
//		}
//		if(!StringUtils.nullToString(time).equals("")) {
//			condition.append(" and time='").append(time).append("'");
//		}
		String latitude = commonReq.getLatitude();
		if(!StringUtils.nullToString(latitude).equals("")) {
			condition.append(" and latitudeE6='").append(latitude).append("'");
		}
		String longitude = commonReq.getLongitude();
		if(!StringUtils.nullToString(longitude).equals("")) {
			condition.append(" and longitudeE6='").append(longitude).append("'");
		}
//		if(!StringUtils.nullToString(phone).equals("")) {
//			condition.append(" and phone='").append(phone).append("'");
//		}
//		if(!StringUtils.nullToString(price).equals("")) {
//			condition.append(" and price='").append(price).append("'");
//		}
		return condition.toString();
	}

	public int insertPark(ParkInfo info) {
		StringBuilder sql = new StringBuilder();
		sql.append("insert into parkinfo(name,street,direction,reference,crossing,total,remain,").append(
				"time,photoUrl,latitudeE6,longitudeE6,phone,price,remark) values('").append(info.getName()).append("','")
				.append(info.getStreet()).append("','").append(info.getDirection()).append("','").append(info.getReference())
				.append("','").append(info.getCrossing()).append("','").append(info.getTotal()).append("','").append(
						info.getRemain()).append("','").append(info.getTime()).append("','").append(info.getPhotoUrl()).append(
						"','").append(info.getLatitudeE6()).append("','").append(info.getLongitudeE6()).append("','").append(
						info.getPhone()).append("','").append(info.getPrice()).append("','").append(info.getRemark()).append("')");
		return this.getJdbcTemplate().update(sql.toString());
	}

	public int updatePark(ParkInfo info) {
		StringBuilder sql = new StringBuilder();
		sql.append("update parkinfo set name='").append(info.getName()).append("',street='").append(info.getStreet())
				.append("',direction='").append(info.getDirection()).append("',reference='").append(info.getReference())
				.append("',crossing='").append(info.getCrossing()).append("',total='").append(info.getTotal()).append(
						"',remain='").append(info.getRemain()).append("',time='").append(info.getTime()).append("',photoUrl='")
				.append(info.getPhotoUrl()).append("',latitudeE6='").append(info.getLatitudeE6()).append("',longitudeE6='")
				.append(info.getLongitudeE6()).append("',phone='").append(info.getPhone()).append("',price='").append(
						info.getPrice()).append("',remark='").append(info.getRemark()).append("' where parkinfoid=").append(
						info.getParkInfoId());
		return this.getJdbcTemplate().update(sql.toString());
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public void test(){
		
		CommonReq.Builder build = CommonReq.newBuilder();
		build.setCurrentPage("1");
		build.setPageSize("5");
		getParkInfoList(build.build());
	}

}
